# -*- coding: utf-8 -*-
import scrapy
import os
from douban.settings import ROOT_DIR
from selenium import webdriver
import time
import random
from utils.re_douban import celebrity_re, movies_re, imdb_re, urlid_re
from utils.bs4_douban import parse_middle_str,parse_staff_str
from douban.items import DoubanItem

class DoubanIndexSpider(scrapy.Spider):
    name = 'douban_index'
    allowed_domains = ['movie.douban.com']

    # start_urls = ['http://https://movie.douban.com/tag/#/?sort=U/']

    def start_requests(self):
        # i = 0
        # js = """
        # document.getElementsByClassName('more')[0].click()
        # """
        # path = os.path.join(ROOT_DIR, 'chromedriver')
        # browser = webdriver.Chrome(executable_path=path)
        # browser.get('https://movie.douban.com/tag/#/?sort=U&range=0,10&tags=%E7%94%B5%E5%BD%B1')
        # time.sleep(10)
        # while i < 100:
        #     browser.execute_script(js)
        #     time.sleep(random.randint(3,4))
        #     i += 1
        #
        # browser.quit()
        start_url = "https://movie.douban.com/subject/30164448/?from=showing"
        yield scrapy.Request(url=start_url, callback=self.parse_detail)

    def parse_index(self, response):
        pass

    def parse_detail(self, response):
        """
        分析详细页数据
        :param response:
        :return:
        """
        # 电影标题 字符串
        title = response.css("span[property='v:itemreviewed']::text").extract_first()
        # 上映日期 list ['2019-03-22(中国大陆)', '2018-11-02(美国)']
        online_time = response.css('span[property="v:initialReleaseDate"]::text').extract()
        # 类型 list
        kind = response.css('span[property="v:genre"]::text').extract()

        # 地区测试等测

        res = parse_middle_str(response.body.decode())
        # 地区 str
        zone = res['zone']
        # 语言 str
        langue = res['langue']
        # 影片长度 两种
        movie_length1 = res['movie_length1']
        movie_length2 = res['movie_length2']

        # 获取imdb连接 str
        imdb_href = None
        for i in response.css('a'):
            href = i.attrib.get('href', None)
            if href:
                imdb_right_href = imdb_re(href)
                if imdb_right_href:
                    imdb_href = imdb_right_href
                    continue
                #
                movie_right_url = movies_re(href)
                if movie_right_url:
                    movie_right_url = "https://movie.douban.com/" + movie_right_url
                    yield scrapy.Request(url=movie_right_url, callback=self.parse_detail)

        # 评分和评论的人数
        try:
            score = response.css("strong.rating_num::text")[0].extract()
        except IndexError:
            score = '0'
        try:
            score_num = response.css("span[property='v:votes']::text")[0].extract()
        except IndexError:
            score_num = '0'
        # 电影简介
        summary = response.css("span[property='v:summary']::text").extract()
        # 封面图
        title_pic = response.css('img[rel="v:image"]').attrib.get('src',None)

        # urlid号
        urlid = urlid_re(response.url)

        # 把所有的信息都存储起来并传递给下面的演员信息 并在演员信息中一起保持
        all_data = {
            "title": title,
            "online_time": online_time,
            "kind": kind,
            "zone": zone,
            "langue": langue,
            "movie_length1": movie_length1,
            "movie_length2": movie_length2,
            "imdb_href": imdb_href,
            "score": score,
            "score_num": score_num,
            "summary": summary,
            "title_pic": title_pic,
            "urlid": urlid
        }
        # 职员信息url response.request._url+"celebrities" 直接拼凑即可 然后进入所有演员列表去爬去信息
        staff_url = response.request._url + "celebrities"
        yield scrapy.Request(url=staff_url, callback=self.parse_staffs, meta={'all_data': all_data})

    def parse_staffs(self, response):
        """
        从staff介入 爬去电影的所有职员列表
        :param response:
        :return:
        """
        staff_data = parse_staff_str(response.body.decode())
        all_data = response.meta['all_data']
        all_data['staff_data'] = staff_data
        item = DoubanItem()
        item['movie_info'] = all_data
        yield item
        # 通过每个人的链接再进入到每个人的详细链接中
        for i in response.css('a'):
            tmp_url = i.attrib.get('href', None)
            if tmp_url:
                right_url = celebrity_re(tmp_url)
                if right_url:
                    celebrity_url = "https://movie.douban.com/" + right_url
                    # yield scrapy.Request(url=celebrity_url,callback=self.parse_staff)
                    # 把进入他个人的电影信息列表 就是他所有参演的电影
                    celebrity_movies_url = celebrity_url + "/movies?sortby=vote&format=pic"
                    yield scrapy.Request(url=celebrity_movies_url, callback=self.parse_staff_movies)

    def parse_staff_movies(self, response):
        """
        每个演员的参与电影详细信息
        :param response:
        :return:
        """
        #
        for i in response.css('a'):
            tmp_url = i.attrib.get('href', None)
            if tmp_url:
                right_url = movies_re(tmp_url)
                if right_url:
                    movie_url = "https://movie.douban.com/" + right_url
                    yield scrapy.Request(url=movie_url, callback=self.parse_detail)
